package com.lsl.bhllserver.service.order;

import java.util.ArrayList;
import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.lsl.bhllserver.entity.Product;
import com.lsl.bhllserver.entity.Order;
import com.lsl.bhllserver.service.func.IOrderBulider;

@Service
public class BusOrderService extends OrderService {

    //@CacheEvict(value = "allOrder", key = "'allOrder'")
    public boolean startPost(IOrderBulider ob) {
        Order temp = orderDAO.findByID(ob.build().getId().toString());
        System.err.println("temp: " + temp.toString());
        temp.setOrderStatus(1);
        return orderDAO.update(temp.getId(), temp);
    }

    //@CacheEvict(value = "allOrder", key = "'allOrder'")
    public boolean finishPost(IOrderBulider ob) {
        Order temp = orderDAO.findByID(ob.build().getId().toString());
        temp.setOrderStatus(2);
        return orderDAO.update(temp.getId(), temp);
    }

    //@CacheEvict(value = "allOrder", key = "'allOrder'")
    public boolean editPost(IOrderBulider ob) {
        Order editOrder = ob.build();
        Order temp = orderDAO.findByID(editOrder.getId().toString());
        editOrder.setProdId(temp.getProdId());
        editOrder.setCusId(temp.getCusId());
        editOrder.setOrderBuyCount(temp.getOrderBuyCount());
        editOrder.setOrderStatus(temp.getOrderStatus());
        editOrder.setOrderDate(temp.getOrderDate());
        //editOrder.setOrderFinishDate(temp.getOrderFinishDate());
        return orderDAO.update(editOrder.getId(), editOrder);
    }

    // @Cacheable(value = "busMyOrder", key = "#userid")
    public List<Order> getMyOrder(Integer userid) {
        List<Product> products = productDAO.findByBusID(userid);
        List<Order> orders = new ArrayList<>();
        for (Product product : products) {
             orders.addAll(orderDAO.findByProdID(String.valueOf(product.getId())));
        }
        return orders;
    }
}
